package com.tencent.cloud.iov.util.storage;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.tencent.mars.xlog.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * preferences 通用工具类，用于持久化的数据，不需要持久化的数据请使用TXCacheManager
 *
 * @author vinsonhuang
 * @date 2018/5/18
 */
public class TXSharedPreferencesUtils {

    private static final String TAG = TXSharedPreferencesUtils.class.getSimpleName();

    private static SharedPreferences sSharedPreferences = null;

    private TXSharedPreferencesUtils() {
    }

    public static void init(Context context) {
        sSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context
                .getApplicationContext());
    }

    public static void setValue(String key, String value) {
        TXCacheManager.getInstance().put(key, value);

        synchronized (TXSharedPreferencesUtils.class) {
            SharedPreferences.Editor editor = sSharedPreferences.edit();
            editor.putString(key, value);
            editor.apply();
        }
    }

    public static String getValue(String key) {
        if (!TXCacheManager.getInstance().containsKey(key)) {
            TXCacheManager.getInstance().put(key, sSharedPreferences.getString(key, ""));
        }
        return TXCacheManager.getInstance().get(key);
    }

    public static void setList(String key, List<String> values) {
        if (null == values || values.isEmpty()) {
            return;
        }
        StringBuilder builder = new StringBuilder();
        int size = values.size();
        for (int i = 0; i < size; ++i) {
            builder.append(values.get(i));
            if (i < size - 1) {
                builder.append(";");
            }
        }
        setValue(key, builder.toString());
    }

    public static List<String> getList(String key) {
        String value = getValue(key);
        if (TextUtils.isEmpty(value)) {
            return null;
        }

        String[] values = value.split(";");
        if (null != values && values.length > 0) {
            List<String> list = new ArrayList<>();
            for (String v : values) {
                list.add(v);
            }
            return list;
        }
        return null;
    }

    public static void setMap(String key, Map<String, String> values) {
        if (null == values || values.isEmpty()) {
            return;
        }
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : values.entrySet()) {
            builder.append(entry.getKey()).append("=").append(entry
                    .getValue()).append("&");
        }
        setValue(key, builder.toString());
    }

    public static Map<String, String> getMap(String key) {
        String value = getValue(key);
        if (TextUtils.isEmpty(value)) {
            return null;
        }

        String[] values = value.split("&");
        if (null != values && values.length > 0) {
            Map<String, String> map = new HashMap<>();
            for (String v : values) {
                String[] keyValue = v.split("=");
                if (null != keyValue && 2 == keyValue.length) {
                    map.put(keyValue[0], keyValue[1]);
                }
            }
            return map;
        }

        return null;
    }

    public static void setBoolean(String key, boolean value) {
        String booleanValue = String.valueOf(value);
        setValue(key, booleanValue);
    }

    public static boolean getBoolean(String key) {
        String value = getValue(key);
        return Boolean.valueOf(value);
    }

    public static void setIntValue(String key, int value) {
        String intValue = String.valueOf(value);
        setValue(key, intValue);
    }

    public static int getIntValue(String key) {
        String value = getValue(key);
        if (TextUtils.isEmpty(value)) {
            return 0;
        }
        int intValue = 0;
        try {
            intValue = Integer.parseInt(value);
        } catch (Exception ex) {
            Log.e(TAG, "parse int failed", ex);
        }
        return intValue;
    }

    public static void remove(String key) {
        if (sSharedPreferences.contains(key)) {
            SharedPreferences.Editor editor = sSharedPreferences.edit();
            editor.remove(key);
            editor.apply();
        }
        TXCacheManager.getInstance().remove(key);
    }
}
